package com.yubest.servletjsp.action;

import com.yubest.servletjsp.dao.UserInfoDao;
import com.yubest.servletjsp.model.UserInfo;
import com.yubest.servletjsp.util.RequestUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@WebServlet(value = "/user.do")
public class UserServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        this.exec(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        this.exec(request, response);
    }

    public void exec(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String method = request.getParameter("method");
        Method m = Method.of(method);
        switch (m) {
            case LOGIN:
                login(request, response);
                break;
            case LOGOUT:
                logout(request, response);
                break;
            case GET_ONE:
                checkLogin(request, response);
                getOne(request, response);
                break;
            case ADD:
                checkLogin(request, response);
                add(request, response);
                break;
            case REMOVE:
                checkLogin(request, response);
                remove(request, response);
                break;
            case EDIT:
                checkLogin(request, response);
                edit(request, response);
                break;
            case LIST:
                checkLogin(request, response);
                list(request, response);
                break;
            default:
                RequestUtil.error("/views/error.jsp", "请求方法不存在！", request, response);
                break;
        }
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        UserInfoDao userInfoDao = new UserInfoDao();
        UserInfo entity = userInfoDao.queryByUsername(username);

        if (null != entity && Objects.equals(entity.getPassword(), password)) {
            HttpSession session = request.getSession();
            session.setAttribute("username", username);
            request.setAttribute("username", username);

            this.list(request, response);
        } else {
            RequestUtil.error("/views/login.jsp", "账号或密码错误", request, response);
        }
    }

    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        HttpSession session = request.getSession();
        session.removeAttribute("username");

        RequestUtil.success("/views/login.jsp", null, request, response);
    }

    public void getOne(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        UserInfoDao userInfoDao = new UserInfoDao();
        UserInfo entity = userInfoDao.queryById(id);
        RequestUtil.success("/views/user/edit.jsp", entity, request, response);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");

        UserInfoDao userInfoDao = new UserInfoDao();
        UserInfo entity = new UserInfo();
        entity.setPhone(phone);
        entity.setUserName(username);
        entity.setPassword(password);
        userInfoDao.add(entity);

        this.list(request, response);
    }

    public void remove(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        UserInfoDao userInfoDao = new UserInfoDao();
        userInfoDao.delete(id);

        this.list(request, response);
    }

    public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");

        UserInfoDao userInfoDao = new UserInfoDao();
        UserInfo entity = new UserInfo();
        entity.setId(id);
        entity.setPhone(phone);
        entity.setUserName(username);
        entity.setPassword(password);
        userInfoDao.updateById(entity);

        this.list(request, response);
    }

    public void list(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        UserInfoDao userInfoDao = new UserInfoDao();
        List<UserInfo> list = userInfoDao.queryAll();
        RequestUtil.success("/views/user/list.jsp", list, request, response);
    }

    public void checkLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        HttpSession session = request.getSession();
        Object username = session.getAttribute("username");
        if (null == username) {
            RequestUtil.error("/views/error.jsp", "请先登陆", request, response);
        }
    }

    enum Method {
        LOGIN, LOGOUT, GET_ONE, ADD, REMOVE, EDIT, LIST, NON;

        static Method of(String name) {
            for (Method value : values()) {
                if (value.name().equalsIgnoreCase(name)) {
                    return value;
                }
            }
            return NON;
        }
    }

    public void destroy() {
    }
}